package com.yjhb.android.sbz.mvp.taskcheck;

import com.google.gson.Gson;
import com.yjhb.android.sbz.entity.TaskOrder;
import com.yjhb.android.sbz.http.HttpObserver;
import com.yjhb.android.sbz.http.HttpTool;
import com.yjhb.android.sbz.http.UploadTool;
import com.yjhb.android.sbz.mvp.ConventionPresenter;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 创建者 zx
 * 创建时间 2020/12/16 11:38
 */
public class TaskCheckPresenter extends ConventionPresenter<TaskCheckView> {
    private final String TAG = "TaskCheckPresenter";

    public TaskCheckPresenter(TaskCheckView taskCheckView) {
        super(taskCheckView);
    }

    @Override
    protected void onDestroy() {

    }


    public void checkTask(String submitId, final boolean reject, String rejectReason, List<LocalMedia> img) {
        if (view != null) {
            view.onWait("正在提交...");
        }
        List<String> rejectImgs = new ArrayList<>();
        UploadTool.OSSUploadImages(img, true,  new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String s) {
                rejectImgs.add(s);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
                HttpTool.request(model.taskCheck(submitId, reject, rejectReason, new Gson().toJson(rejectImgs)), new HttpObserver<Object>() {
                    @Override
                    public void onResult(Object data) {
                        if (view != null) {
                            view.onSuccess(reject);
                        }
                    }

                    @Override
                    public void onError(int code, String msg) {
                        if (view != null) {
                            view.onFailure(code, msg);
                        }
                    }
                });
            }
        });

    }


    public void succeedTheSameTask(Integer orderId) {
        if (view != null) {
            view.onWait("请稍候..");
        }
        if (model != null)
            HttpTool.request(model.succeedTheSameTask(orderId), new HttpObserver<Integer>() {
                @Override
                public void onResult(Integer data) {
                    if (view != null) {
                        view.onPassRecordNum(data);
                    }
                }

                @Override
                public void onError(int code, String msg) {
                    if (view != null) {
                        view.onFailure(code, msg);
                    }
                }
            });
    }


    public void userAllOrder(int index,Integer taskId,Integer userId,Integer orderId){
        if (model!=null){
            HttpTool.request(model.userAllOrder(taskId, userId,orderId), new HttpObserver<List<TaskOrder>>() {
                @Override
                public void onResult(List<TaskOrder> data) {
                    if (view!=null){
                        view.onTaskOrders(index,data);
                    }
                }

                @Override
                public void onError(int code, String msg) {
                    if (view!=null){
                        view.onFailure(code, msg);
                    }
                }
            });
        }
    }

}
